package com.ohs.fo.action;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.ohs.bll.dto.SearchResultDTO;
import com.ohs.bll.service.LocationService;
import com.ohs.bll.service.MailService;
import com.ohs.bll.service.RoomService;
import com.ohs.bll.service.VideoService;
import com.ohs.bll.util.CommonUtil;
import com.ohs.bll.util.ConfigurationManager;
import com.ohs.bll.util.Constant;
import com.ohs.dal.model.Location;
import com.ohs.dal.model.Province;
import com.ohs.dal.model.Room;
import com.ohs.dal.model.Specialty;
import com.ohs.dal.model.Video;

public class HomeAction extends AbstractAction {
	
	private static final long serialVersionUID = 1L;

	private static final int RESULT_PER_PAGE = 20;
	
	private static final int NUM_OF_SHOWED_PAGE = 15;
	
	private static final int TOP_VIDEOS = 30;
	
	private LinkedHashMap<String, Object> jsonData = new LinkedHashMap<String, Object>();

	private List<Room> listRoom = new ArrayList<Room>();
	
	private List<Province> provinces = new ArrayList<Province>();
	
	private List<Specialty> specialties = new ArrayList<Specialty>();
	
	private List<Location> topLocations;
	
	private List<Video> topVideos;
	
	private long selectedProvinceId;
	
	private long selectedSpecialtyId;
	
	private LocationService locationService;

	private RoomService roomService;
	
	private MailService mailService;
	
	private VideoService videoService;
	
	private List<SearchResultDTO> result;
	
	private String keyword;
	
	private String name;
	
	private String email;
	
	private String body;
	
	private String captcha;
	
	private int page;
	
	private int totalPage;
	
	private int begin;
	
	private int end;

	public LinkedHashMap<String, Object> getJsonData() {
		return jsonData;
	}

	public String home() throws Exception {
		topLocations = locationService.getTopLocation(20);
		topVideos = videoService.getLatestVideos(TOP_VIDEOS);
		return "success";
	}
	
	public String commonError() throws Exception {
		
		return "error";
	}
	
	public String about() throws Exception {
		return "success";
	}
	
	public String search() throws Exception {
		// hack for auto disconnect from db issue
		try {
			locationService.getAllProvince();
		} catch (Exception e) {
			log.error("CAN'T CONNECT DB");
		}
		
		if (keyword == null) {
			keyword = "";
			selectedProvinceId = -1;
			selectedSpecialtyId = -1;
		}
		
		if (page == 0) {
			page = 1;
		}
		provinces.add(new Province(-1, "Tất cả"));
		provinces.addAll(locationService.getAllProvince());
		specialties.add(new Specialty(-1, "Tất cả"));
		specialties.addAll(locationService.getAllSpecialty());
		
		int total = locationService.searchTotal(keyword, selectedProvinceId, selectedSpecialtyId);
		totalPage = (total % RESULT_PER_PAGE == 0) ? total/RESULT_PER_PAGE : total/RESULT_PER_PAGE + 1;
		result = locationService.search(keyword, (page - 1) * RESULT_PER_PAGE, RESULT_PER_PAGE, selectedProvinceId, selectedSpecialtyId);
		
		begin = ((page - 7) > 0) ? page - 7 : 1;
		end = ((page + 7) < totalPage) ? page + 7 : totalPage;
		return "success";
	}
	
	public String contact() throws Exception {
		return "success";
	}
	
	public String contactSubmit() throws Exception {
		if (CommonUtil.checkCaptcha(ServletActionContext.getRequest().getSession(), captcha)) {
			HttpServletRequest request = ServletActionContext.getRequest();
			String user = (String) request.getSession().getAttribute(Constant.USER_NAME);
			String preSubject = "";
			if (user != null && !"".equals(user)) {
				preSubject += user;
			}
			mailService.sendMail(ConfigurationManager.getAsString(ConfigurationManager.CONFIG, "username"), "From" + preSubject + ": " + name + "(" + email + ")", body);
		}
		return "success";
	}
	
	public static String addPageParam(String url) {
		if (url.contains("page=")) {
			url = url.replaceAll("page=\\d+", "");
		}
		return url.contains("?") ? url + "&page=" : url + "?page=";
	}
	
	public List<Province> getProvinces() {
		return provinces;
	}

	public List<Location> getTopLocations() {
		return topLocations;
	}

	public void setTopLocations(List<Location> topLocations) {
		this.topLocations = topLocations;
	}

	public VideoService getVideoService() {
		return videoService;
	}

	public void setVideoService(VideoService videoService) {
		this.videoService = videoService;
	}

	public void setProvinces(List<Province> provinces) {
		this.provinces = provinces;
	}

	public List<Specialty> getSpecialties() {
		return specialties;
	}

	public void setSpecialties(List<Specialty> specialties) {
		this.specialties = specialties;
	}

	public void setJsonData(LinkedHashMap<String, Object> jsonData) {
		this.jsonData = jsonData;
	}

	public LocationService getLocationService() {
		return locationService;
	}

	public long getSelectedProvinceId() {
		return selectedProvinceId;
	}

	public void setSelectedProvinceId(long selectedProvinceId) {
		this.selectedProvinceId = selectedProvinceId;
	}

	public long getSelectedSpecialtyId() {
		return selectedSpecialtyId;
	}

	public void setSelectedSpecialtyId(long selectedSpecialtyId) {
		this.selectedSpecialtyId = selectedSpecialtyId;
	}

	public List<Video> getTopVideos() {
		return topVideos;
	}

	public void setTopVideos(List<Video> topVideos) {
		this.topVideos = topVideos;
	}

	public int getBegin() {
		return begin;
	}

	public void setBegin(int begin) {
		this.begin = begin;
	}

	public int getEnd() {
		return end;
	}

	public void setEnd(int end) {
		this.end = end;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setLocationService(LocationService locationService) {
		this.locationService = locationService;
	}

	public RoomService getRoomService() {
		return roomService;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public void setRoomService(RoomService roomService) {
		this.roomService = roomService;
	}

	public List<SearchResultDTO> getResult() {
		return result;
	}

	public void setResult(List<SearchResultDTO> result) {
		this.result = result;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public MailService getMailService() {
		return mailService;
	}

	public void setMailService(MailService mailService) {
		this.mailService = mailService;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getCaptcha() {
		return captcha;
	}

	public void setCaptcha(String captcha) {
		this.captcha = captcha;
	}

}